import qs from "webopenfather-qs"; // pnpm add  webopenfather-qs

const baseURL = "/fmcrm";

export const request = <T = any>(
  path: string,
  options: REQUEST.Options
): Promise<T> => {
  const { method = "GET", headers = {}, body = {}, params = {} } = options;
  return fetch(baseURL + path + "?" + qs.stringify(params), {
    method,
    headers: {
      "content-type": "application/x-www-form-urlencoded",
      token:
        localStorage.getItem("token") ||
        "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJpYXQiOjE2OTE5OTYyOTN9.ZY97mAus5VQm5QWqT8hkmOpWr6UlaztqVOMRGD4nMjk",
      ...headers,
    },
    body: Object.keys(body).length ? qs.stringify(body) : null,
  }).then((res) => res.json());
};
